package turtle.system.invest.rule;

import java.io.Serializable;

public class InvestHistory implements Serializable {
	private static final long serialVersionUID = 3210372378269644594L;

	private static final int NTD_PER_POINT = 2;
	
	private long deposit = 0;
	private int hold = 0;
	private double fee;
	private double taxRate;
	private int tradeCount;
	
	public void setFeeAndTaxRate(double fee, double taxRate) {
		this.fee = fee;
		this.taxRate = taxRate;
	}

	public boolean adjustTo(int unit, int price, int timestamp) {
		if (price < 0) 
			throw new IllegalArgumentException();
	    if (unit == hold) return false;
	    ++ tradeCount;
/*	    System.out.println(String.format(
	    		"adjust from %s to %s at %s [%s, %s]", 
	    		hold, unit, price, timestamp, getProfit(price)));*/
	    deposit -= (long) (unit - hold) * price * NTD_PER_POINT;
	    deposit -= Math.round(Math.abs(unit - hold) * (fee + price * taxRate));
        hold = unit;
		return true;
	}

	public void log(int timestamp, String message) {
		System.out.println(String.format("%s [%s]", message, timestamp));
	}

	public long getDeposit() {
	    return this.deposit;
	}

	public long getProfit(int price) {
	    return price * hold * NTD_PER_POINT + deposit;
	}

    public int getHoldPosition() {
        return this.hold;
    }
    
}
